import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class search_pro{
	int select[];
	int best[];
	
	int replacekey[];
	int splitnum;
	int turlnum;
	//int gun=0;
	int newoptionnum=0;
	String newoption=null;
	String html;
	
	String hoption[]=null;
	
	String turl;
	String key;//="ȫ�浿";
	String http;//="http://chulic.cheju.ac.kr//DLiWeb20fr/components/searchir/result.aspx?";
	String str[]; //= {"m_var=421","srv_id=31","qy_frm=QUICK","qy_typ=KEYWORD","qy_idx=TITL","qy_kwd=%ED%99%8D%EA%B8%B8%EB%8F%99","brch=ALL"};
	search_pro(String str, String key, String http, String strstr[])
	{
		this.turl=str;
		this.key = key;
		this.http= http+"?";
		this.str=strstr;
		this.splitnum=strstr.length;
		select = new int[strstr.length];
		best= new int[strstr.length];
		replacekey= new int[strstr.length];
		StringBuilder html = new StringBuilder(); 
		try {
			URL url = new URL(turl);
			HttpURLConnection conn = null; 
            if (url.getProtocol().toLowerCase().equals("https")) { 
            } else { 
            	conn = (HttpURLConnection) url.openConnection(); 
            }
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				int resultcode = conn.getResponseCode();
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));	
					for (;;) {						
						String line = br.readLine();				
						if (line == null) break;
						html.append(line + '\n'); 
					}
					br.close();
				}
				conn.disconnect();
			}
			System.out.println(html.toString());
			
		} 
		catch (Exception ex) {
			}
		this.html=html.toString();
		this.turlnum=findkwd(html.toString(),key,0);
	}
	public int count(int how[])
	{
		int con=0;
		for(int i = 0 ; i < how.length; i++)
			if(how[i]==0)
				con++;
		return con;
	}
	public String hap(int select[])
	{
		String sol= http;
		for(int i = 0 ; i < select.length; i++)
			if(select[i]==0){
				//System.out.println(select[i]);
				sol=sol+"&"+str[i];
			}
		return sol;
	//	System.out.println(sol);
	}
	public String hap2()
	{
		String sol= http;
		for(int i = 0 ; i < best.length; i++)
			if(best[i]==0){
				//System.out.println(select[i]);
				sol=sol+"&"+str[i];
			}
		return sol;
	//	System.out.println(sol);
	}
	public int findkwd(String turl, String kwd, int indexnum  )
	{
		int k,count=0;
		k=turl.indexOf(kwd, indexnum);
		while(k!=-1)
		{
			count++;
			k=turl.indexOf(kwd,k+1);
		}
		return count;
	}
	public void newbt()
	{
		for(int i=0; i < select.length; i++)
		{
			select[i]=1;
			if(getkwdnum()==turlnum)
			{
				best[i]=1;
				System.out.println("best["+i+"]=1");
				System.out.println("------------");
			}
			else
			{
				best[i]=0;
				System.out.println("best["+i+"]=0");
				System.out.println("------------");
			}
			select[i]=0;
		}
	}
	public int getkwdnum()
	{
		StringBuilder html2 = new StringBuilder(); 
		try {
			URL url2 = new URL(hap(select));
			
			System.out.println(hap(select));
			
			HttpURLConnection conn2 = null; 
            if (url2.getProtocol().toLowerCase().equals("https")) { 
            } else { 
            	conn2 = (HttpURLConnection) url2.openConnection(); 
            } 		
			if (conn2 != null) {
				conn2.setConnectTimeout(10000);
				conn2.setUseCaches(false);
				int resultcode2 = conn2.getResponseCode();
				if (conn2.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br2 = new BufferedReader(new InputStreamReader(conn2.getInputStream(),"UTF-8"));					
					for (;;) {					
						String line2 = br2.readLine();
						if (line2 == null) break;
						html2.append(line2 + '\n'); 
					}
					br2.close();
				}
				conn2.disconnect();
			}
		}
		catch (Exception ex) {
		}
		System.out.println(turlnum+" => "+findkwd(html2.toString(),key,0));
		return findkwd(html2.toString(),key,0);
	}
	public void type(String typest){
		System.out.println(html);
		System.out.println(typest);
		Document doc = Jsoup.parse(html);
		Elements links = doc.select("select");
		for (Element link : links) {
			String linkInnerH = link.html();
			Document op = Jsoup.parse(linkInnerH);
			String SelectId = link.attr("name");
			System.out.println("select id => " + SelectId);
			Elements options = op.select("option");
			int cnt=0;
			
			for(int k=0;k<select.length;k++)
			{
				if(str[k].contains(SelectId))
				{				
					for (Element option : options) {
							cnt++;
					}
				}
			}
			hoption = new String[cnt];
			cnt=0;
			for(int k=0;k<select.length;k++)
			{
				if(str[k].contains(SelectId))
				{
					newoptionnum=k;
					
					for (Element option : options) {
						String optionInnerH = option.html();					
						//System.out.println("옵션 : " + optionInnerH);
						
						hoption[cnt]=optionInnerH;
						System.out.println(hoption[cnt]);
						String optionvalue = option.attr("value");
						//System.out.println("옵션 value : " + optionvalue);
						if (optionInnerH.equals(typest)) {
							newoption = "&"+SelectId+"="+optionvalue;
						}
						cnt++;
					}
					
					
				}
			}
		}
	}
	public void ktype() throws UnsupportedEncodingException
	{
		for(int k=0 ; k < str.length; k++)
		{
			String[] rstr = str[k].split("=");
			if(rstr.length == 2){
				if(rstr[1].equals(key))
				{
					replacekey[k]=1;
				}
				if(rstr[1].equals(URLEncoder.encode(key,"utf-8")))
				{
					replacekey[k]=2;
				}
			}
		}
	}
	public void replaceboth()
	{
		System.out.println(newoption);
		for(int i =0 ; i < str.length; i++){
			if(replacekey[i]==2||replacekey[i]==1)
				System.out.println(str[i]);
		}
	}
}
